package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.DrawActivity;
import cn.com.connext.scrm.domain.master.NewDrawActivity;
import cn.com.connext.scrm.repository.master.DrawActivityRepository;
import cn.com.connext.scrm.repository.master.NewDrawActivityRepository;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.web.rest.vm.NewDrawActivityAddVM;
import cn.com.connext.scrm.web.rest.vm.NewDrawActivityVM;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.ZonedDateTime;
import java.util.LinkedList;
import java.util.List;

@Service
@Transactional
public class NewDrawActivityService {

    @Autowired
    private NewDrawActivityRepository newDrawActivityRepository;
    @Autowired
    private DrawActivityRepository drawActivityRepository;


    public Page<NewDrawActivity> findByPage(NewDrawActivityVM wocv) {
        PageRequest pageRequest = new PageRequest(wocv.getPage(), wocv.getPageSize(), Sort.Direction.DESC, "createTime");
        Page<NewDrawActivity> page = newDrawActivityRepository.findAll(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                List<Predicate> list = new LinkedList<>();
//                Predicate activityName = cb.equal(root.get("isDelete"), 2);
//                list.add(activityName);
                Predicate[] pre = new Predicate[list.size()];
                return query.where(list.toArray(pre)).getRestriction();
            }
        }, pageRequest);
        return page;
    }

    public JsonResult save(NewDrawActivity entity) {
        JsonResult result = new JsonResult();
        NewDrawActivity resultNewDrawActivity;
        if(entity !=null){
            if (entity.getId() !=null) {
                resultNewDrawActivity = newDrawActivityRepository.findOne(entity.getId());
            } else {
                resultNewDrawActivity = new NewDrawActivity();
                resultNewDrawActivity.setCreateTime(ZonedDateTime.now());
            }
            if(StringUtils.isNotEmpty(entity.getPath())){
                resultNewDrawActivity.setPath(entity.getPath());
            }
            if(StringUtils.isNotEmpty(entity.getCampaignID())){
                resultNewDrawActivity.setCampaignID(entity.getCampaignID());
            }
            if(StringUtils.isNotEmpty(entity.getSource())){
                resultNewDrawActivity.setSource(entity.getSource());
            }
            if(StringUtils.isNotEmpty(entity.getSourceContent())){
                resultNewDrawActivity.setSourceContent(entity.getSourceContent());
            }
            if(StringUtils.isNotEmpty(entity.getDrawActivityId())){
                resultNewDrawActivity.setDrawActivityId(entity.getDrawActivityId());
                DrawActivity one = drawActivityRepository.findOne(entity.getDrawActivityId());
                if(one !=null){
                    if(StringUtils.isNotEmpty(entity.getCampaignID())){
                        one.setCampaignID(entity.getCampaignID());
                    }
                    if(StringUtils.isNotEmpty(entity.getSource())){
                        one.setSource(entity.getSource());
                    }
                    if(StringUtils.isNotEmpty(entity.getSourceContent())){
                        one.setSourceContent(entity.getSourceContent());
                    }
                    drawActivityRepository.saveAndFlush(one);
                }

            }
            newDrawActivityRepository.saveAndFlush(resultNewDrawActivity);
            result.setMessage("操作成功");
            result.setState(0);
        }else{
            result.setMessage("操作失败，参数异常");
            result.setState(-1);
        }
        return result;
    }

    public JsonResult delete(NewDrawActivity entity) {
        JsonResult result = new JsonResult();
        if(entity !=null && entity.getId() !=null){
            newDrawActivityRepository.delete(entity.getId());
            result.setMessage("删除成功");
            result.setState(0);
        }else{
            result.setMessage("操作失败，参数异常");
            result.setState(-1);
        }
        return result;
    }

    public JsonResult findByPath(NewDrawActivity entity) {
        JsonResult result = new JsonResult();
        NewDrawActivity resultNewDrawActivity = newDrawActivityRepository.findByPath(entity.getPath());
        result.setData(resultNewDrawActivity);
        result.setState(0);
        return result ;
    }

    public List<NewDrawActivity> findByDrawActivityId(String drawActivityId) {
        List<NewDrawActivity> byDrawActivityId = newDrawActivityRepository.findByDrawActivityId(drawActivityId);
        return byDrawActivityId;
    }

    public void newAdd(NewDrawActivityAddVM entity) {
        List<NewDrawActivityVM> newDrawActivityList =entity.getNewDrawActivityList();
            for (NewDrawActivityVM newDrawActivityVM : newDrawActivityList) {
                NewDrawActivity newDrawActivity = new NewDrawActivity();
                if (StringUtils.isNotBlank(newDrawActivityVM.getCampaignID())){
                    newDrawActivity.setCampaignID(newDrawActivityVM.getCampaignID());
                }
                if (StringUtils.isNotBlank(newDrawActivityVM.getDrawActivityId())){
                    newDrawActivity.setDrawActivityId(newDrawActivityVM.getDrawActivityId());
                }
                if (StringUtils.isNotBlank(newDrawActivityVM.getPath())){
                    newDrawActivity.setPath(newDrawActivityVM.getPath());
                }
                if (StringUtils.isNotBlank(newDrawActivityVM.getSource())){
                    newDrawActivity.setSource(newDrawActivityVM.getSource());
                }
                if (StringUtils.isNotBlank(newDrawActivityVM.getSourceContent())){
                    newDrawActivity.setSourceContent(newDrawActivityVM.getSourceContent());
                }
                if (null != newDrawActivityVM.getId()){
                    newDrawActivity.setId(newDrawActivityVM.getId());
                }
                this.save(newDrawActivity);
            }
    }
}
